
const { SIGNAL_DEFINE } = require("./signal");

const clientMgr = require("../clientMgr");
const socket = require('../../base_net/socket');
const utils = require("../../utils/utils");

const GAME_TYPE = {
    NN: "wd_nn",
    DX: "pk_dx",
    ZJH: "pk_zjh",
    SG: "pk_sg",
}

class DXNetMgr {
    constructor() {
        this.signal = null;
        this.gameMgr = null;
        this.seatMgr = null;
    }

    setSignal(signal) {
        this.signal = signal;
        this.registerSignals();
    }

    registerSignals() {
        this.signal.register(SIGNAL_DEFINE.USER_READY, this.onUserReady.bind(this));
        this.signal.register(SIGNAL_DEFINE.USER_SIT, this.onUserSit.bind(this));
        this.signal.register(SIGNAL_DEFINE.USER_EXIT, this.onUserExit.bind(this));
        this.signal.register(SIGNAL_DEFINE.GAME_BEGIN, this.onGameBegin.bind(this));
        this.signal.register(SIGNAL_DEFINE.INIT_GAMBER_INFO, this.onInitGamberInfo.bind(this));
        this.signal.register(SIGNAL_DEFINE.DECIDE_BANKER, this.onDecideBanker.bind(this));
        this.signal.register(SIGNAL_DEFINE.BETTING_TURN, this.onBettingTurn.bind(this));
        this.signal.register(SIGNAL_DEFINE.SHOW_CARD, this.onShowCard.bind(this));
        this.signal.register(SIGNAL_DEFINE.BETTING, this.onBetting.bind(this));
        this.signal.register(SIGNAL_DEFINE.NEXT_GAME, this.onNextGame.bind(this));
    }

    setGameMgr(gameMgr) {
        this.gameMgr = gameMgr;
    }
    
    setSeatMgr(seatMgr) {
        this.seatMgr = seatMgr;
    }

    onUserReady(userId, value) {
        clientMgr.broadcastInRoom(socket.resp.user_ready,{userId:userId,ready:value},userId,true);
    }

    onUserSit(userId, isSit) {
        clientMgr.broadcastInRoom(socket.resp.user_sit,{userId:userId,sit:isSit},userId,true);
    }

    onUserExit(userId) {
        clientMgr.sendMsg(userId,socket.resp.exit_result,userId);
        //通知其它玩家，有人退出了房间
        clientMgr.broadcastInRoom(socket.resp.exit_notify,userId,userId,false);
    }

    onGameBegin(currentRound) {
        var users = this.seatMgr.getUsers();
        for (let i = 0; i < users.length; ++i) {
            let user = users[i];
            if (user == null) continue;
            clientMgr.sendMsg(user.userId, socket.resp.game_begin);
            clientMgr.sendMsg(user.userId, socket.resp.game_num, currentRound);
        }
    }

    onInitGamberInfo(gambers) {
        for (let i = 0; i < gambers.length; ++i) {
            if (gambers[i] == null) continue;

            var gamberInfos = {};
            for (let j = 0; j < gambers.length; ++j) {
                if (gambers[j] == null) continue;
                
                var data = {userId: gambers[j].userId, sit: this.seatMgr.isUserSitBySeatIndex(j) ? 1 : 0}
                if (gambers[j].userId == gambers[i].userId && this.seatMgr.isUserSitBySeatIndex(j)) {
                    data.holds = utils.clone(gambers[j].holds);
                } else {
                    data.holds = [-1, -1, -1];
                }
                gamberInfos.push(data);
            }
            clientMgr.sendMsg(userId, socket.resp.game_holds, gamberInfos);
        }
    }

    onDecideBanker(userId, robSeatIndexs) {
        var userArr = [];
        for (let i = 0; i < robSeatIndexs.length; ++i) {
            var userId = this.seatMgr.getUserIdBySeatIndex(robSeatIndexs[i]);
            userArr.push(userId);
        }
        clientMgr.broadcastInRoom(socket.resp.dingzhuang_all,{time:this.gameMgr.decideBankerTime,button:userId,userArr:userArr}, userId, true);
    }

    onBettingTurn(userId, optionalOperates, extraData) {
        var data = {time:this.gameMgr.bettingTime,gambler:userId,operates:optionalOperates};
        if (extraData != null) {
            data.update(extraData);
        }
        clientMgr.broadcastInRoom(socket.resp.turn_betting_notify, data, userId, true);
    }

    onBetting(userId, value) {
        clientMgr.broadcastInRoom(socket.resp.xiazhu_notify,{userId:userId,value:value},userId,true);
    }

    onShowCard() {
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        for (let i = 0; i < users.length; ++i) {
            var index = gamberIndexs[i];
            var gamber = this.seatMgr.getGamberBySeatIndex(index);
            clientMgr.broadcastInRoom(socket.resp.liangpai_notify, {userId: gamber.userId, holds: gamber.holds, sit:gamber.sit}, gamber.userId, true);
            clientMgr.sendMsg(gamber.userId, socket.resp.bipai_all, {time: this.gameMgr.showCardTime});
        }
    }

    onGameOver(results, record, isRoomOver) {
        var endTime = Date.now();
        clientMgr.broadcastInRoom2(socket.resp.game_over,{results:results,endInfo:isRoomOver,endTime:endTime,record:record}, this.gameMgr.getRoomId());
        if (isRoomOver) {
            clientMgr.kickAllInRoom(this.gameMgr.getRoomId());
        }
    }

    onNextGame() {
        clientMgr.broadcastInRoom2(socket.resp.next_all,{time:this.gameMgr.nextGameTime}, this.gameMgr.getRoomId(), true);
    }

    getGameType() {
        return GAME_TYPE.DX;
    }
}

exports.DXNetMgr = DXNetMgr;